//app.js
App({
  globalData: {
    userInfo: null,
   // allUrl: "http://192.168.1.199:8080/PDHJ",
    allUrl: "https://www.tasteofgermany.cn/PDHJ",
    imageUrl:"https://www.tasteofgermany.cn/PDHJ/cargo/",
   // imageUrl:"http://192.168.1.113/PDHJ/cargo/",
    referrerId: -1,
    aCity : { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外" } 
  },
  onLaunch: function () {
    //调用API从本地缓存中获取数据
    // var logs = wx.getStorageSync('logs') || []
    // logs.unshift(Date.now())
    // wx.setStorageSync('logs', logs)
  },
  onLoad: function () {

  },
  showShareMenus: function () {
    wx.showShareMenu({
      withShareTicket: true,
    })
  },
  getUserInfo: function (cb) {//获取用户的唯一 openid
    var that = this;
    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      //调用登录接口
      wx.login({
        success: function (rese) {
          wx.getUserInfo({
            success: function (res) {
              that.globalData.hasRoot = false;
              that.globalData.noRoot = true;
              wx.setStorageSync('userInfo', res.userInfo)//将用户信息储存在本
              that.globalData.userInfo = res.userInfo;
              var reg =/[A-Za - z0 - 9_\-\u4e00 - \u9fa5]+/
              if (!reg.test(res.userInfo.nickName)){
                res.userInfo.nickName=="默认用户名"
              }
              wx.request({
                url: that.globalData.allUrl + "/utilAction/wxLogin",
                data: {
                  code: rese.code,
                  username: res.userInfo.nickName,
                  headportrait: res.userInfo.avatarUrl,
                  referrerId: that.globalData.referrerId,
                },
                method: 'GET',
                success: function (res) {
                  console.log(res.data)
                  var userTypeName;
                  that.globalData.referrerId = res.data;
                  wx.setStorageSync('userid', res.data.id);
                  wx.setStorageSync('userType', res.data.isMerchant);
                  wx.setStorageSync('userAgentId', res.data.agentId);
                  wx.setStorageSync('remoteUserDetail', res.data);
                  wx.setStorageSync("userBalance", res.data.balance);  
                  wx.setStorageSync("agentId", res.data.agentId); 
                  if (res.data.isMerchant==1){
                    userTypeName="体验庄主"
                  } else if (res.data.isMerchant == 2) {
                    userTypeName = "禾怡庄主"
                  } else if (res.data.isMerchant == 3) {
                    userTypeName = "古堡庄主"
                  } else if (res.data.isMerchant == 4) {
                    userTypeName = "金钥匙庄主"
                  } else if (res.data.isMerchant == 5) {
                    userTypeName = "管理员庄主"
                  }
                  wx.setStorageSync('userTypeName', userTypeName);
                }, fail: function () {
                }
              })
            }, fail: function () {
              that.globalData.hasRoot = true;
              that.globalData.noRoot = false;
            }
          })
        }
      })
    }
  }
})